<template>
  <div>
    打开控制台查看
  </div>
</template>

<script>
import { Socket } from "@/utils/webSocket";
export default {
  data() {
    return {
      Socket: null,
      getSocketData: null
    };
  },
  mounted() {
    this.useSocket();
  },
  methods: {
    useSocket() {
      // 消息监听的方法
      this.getSocketData = (data) => {
        const { data: msg } = data?.detail;
        console.log(msg);
        // ....  msg 接收的消息
      };

      // 连接 socket
      this.Socket = new Socket();
      this.Socket.createSocket(`ws:/192.168.14.37:9999/ws/netty/websocket/SK5581`);

      // 消息监听
      window.addEventListener("onmessageWS", this.getSocketData);

      // 注销
      this.$once("hook:beforeDestroy", () => {
        window.removeEventListener("onmessageWS", this.getSocketData);
        this.Socket.closeWS();
      });
    }
  }
};
</script>

<style scoped lang="scss">

</style>
